
window.onload = function () {
  //获取token
  const token = sessionStorage.getItem("token")
  //发送ajax
  axios({
      url:'http://localhost:8080/admin/classInfo/queryAll',
      method: "post",
      
  })
      .then(res => {
          if (res.data.code == 200) {
             classRender(res.data.data)
          } 
      })
  //获取数据,将渲染封装渲染
}


//封装student渲染
function classRender(data) {
  const tbody = document.querySelector(".add")
  tbody.innerHTML = ""
  //渲染
  data.forEach((item, index) => {
    let str = `
              <tr>
              <td>${index + 1}</td>
            <td>${item.cname}</td>
                <td>
               <button class="btn btn-danger btn-sm" data-id = ${item.cid}>删除</button>
     <button class="btn btn-warning btn-sm" data-id = ${item.cid} data-bs-toggle="modal" data-bs-target="#updateclass">修改</button>
    </td>
             </tr>
            `
      tbody.innerHTML += str;
  })
}





// 事件委托
const tbody = document.querySelector('.add')
tbody.addEventListener('click', (e) => {
  /* 
      TODO 删除
 */
  if (e.target.className.includes('danger')) {
    const res = window.confirm("您确定要删除吗？")
    if (res) {
      //获取token
      const token = sessionStorage.getItem('token');
      const cid = e.target.dataset.id
      
      axios({
        url: 'http://localhost:8080/admin/classInfo/del',
        method: 'post',
        headers: { token },
        data: {
          cid
        }
      })
        .then(res => {
          location.reload();
        })
    }
  }



  /* 
TODO 根据ID获取图书信息
*/ 
if (e.target.className.includes('warning')) {
 
    //获取token
    const token = sessionStorage.getItem('token');
    const cid = e.target.dataset.id;
    axios({
      url: 'http://localhost:8080/admin/classInfo/queryOne',
      method: 'post',
      headers: { token },
      data: {
        cid
      }
    })
      .then(res => {
        // console.log(res.data.data.name)
       document.getElementById('name').value = res.data.data.cname;
       document.getElementById('cid').value = res.data.data.cid;
      })
    }

})



/* 
TODO 修改
*/
function updateClass() {
  const token = sessionStorage.getItem('token');
  const aid = document.getElementById('cid').value;
  const cname = document.getElementById('name').value;
  axios({
    url: 'http://localhost:8080/admin/classInfo/update',
    method: 'post',
    headers: { token },
    data: {
      aid,
      cname
    }
  })
    .then(res => {
      location.reload()
    })
}





/* 
TODO 添加
*/
function addClass() {
  // 获取数据
  const tbody = document.querySelector('.add')
  const cname = document.getElementById('cname').value.trim();

  //  console.log(value)
  //发送请求
  const token = sessionStorage.getItem('token');
  axios({
    url: 'http://localhost:8080/admin/classInfo/add',
    method: 'post',
    headers: { token },
    data: {
      cname
    }
  })
    .then(res => {
      location.reload();
    })
}



// 模糊查询
function queryByLikeName(){
  const cname = document.getElementById('likename').value.trim()
  //判断cname
  const token = sessionStorage.getItem("token")
  axios({
    url:"http://localhost:8080/admin/classInfo/likename",
        headers:{token},
        data:{cname},
        method:'post'
  })
  .then(res=>{
    //渲染
    classRender(res.data.data)
  })
}